package neusoft.example.com.yuejian06.Adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.example.com.yuejian06.Activity.ActivityDetailActivity;
import neusoft.example.com.yuejian06.Activity.PictureDetailActivity;
import neusoft.example.com.yuejian06.Bean.Activity;
import neusoft.example.com.yuejian06.PublicActivity;
import neusoft.example.com.yuejian06.R;

public class ActivityListAdapter extends RecyclerView.Adapter{
    private PublicActivity publicActivity;
    private List<Activity> activityList;

    public void setData(List<Activity> list){
        activityList=list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View itemView=inflater.inflate(R.layout.activity_item,parent,false);
        ItemViewHolder viewHolder=new ItemViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        publicActivity=new PublicActivity();
        final ItemViewHolder viewHolder=(ItemViewHolder)holder;
        final Activity activity=activityList.get(position);

        String activity_title=activity.getActivity_title();
        String activity_time=activity.getActivity_time();
        String activity_intro=activity.getActivity_intro();


        viewHolder.activity_name.setText(activity_title);
        viewHolder.activity_time.setText(activity_time);
        viewHolder.activity_intro.setText(activity_intro);

        Picasso.with(viewHolder.imageView.getContext())
                .load(publicActivity.getIp()+"static/picture/"+ activity.getActivity_pic())
                .into(viewHolder.imageView);


        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos=viewHolder.getLayoutPosition();
                Intent intent=new Intent(v.getContext(), ActivityDetailActivity.class);
                intent.putExtra("activity_id",activity.getActivity_id());
                v.getContext().startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        if(activityList!=null)
            return activityList.size();
        else
            return 0;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        public TextView activity_name,activity_time,activity_intro;
        public View itemView;
        public ItemViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            imageView=(ImageView)itemView.findViewById(R.id.pic);
            activity_name=(TextView)itemView.findViewById(R.id.name);
            activity_time=(TextView)itemView.findViewById(R.id.time);
            activity_intro=(TextView)itemView.findViewById(R.id.intro);
           // activity_address=(TextView)itemView.findViewById(R.id.address);
        }
    }

}

